package com.whw.cbf_qimodazuoye.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.whw.cbf_qimodazuoye.R;
import com.whw.cbf_qimodazuoye.service.UserService;
import com.whw.cbf_qimodazuoye.utils.FaSongQingQiu;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;


public class RegisterActivity extends Activity {

    EditText et_userId;
    EditText et_userName;
    EditText et_userPwd;
    EditText et_userPhone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        et_userId=(EditText)findViewById(R.id.userId);
        et_userName =(EditText)findViewById(R.id.userName);
        et_userPwd=(EditText) findViewById(R.id.userPwd);
        et_userPhone=(EditText)findViewById(R.id.userPhone);
        Button btnregister = (Button) findViewById(R.id.register);
        btnregister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String userId=et_userId.getText().toString();
            String userName=et_userName.getText().toString();
            String userPwd=et_userPwd.getText().toString();
            String userPhone=et_userPhone.getText().toString();
            register(userId,userName,userPwd,userPhone);
        }
    });
}
    public void register(String userId,String userName,String userPwd,String userPhone){
        Retrofit retrofit = FaSongQingQiu.createRetrofit();
        UserService userService = retrofit.create(UserService.class);
        Call<ResponseBody> call = userService.register(userId,0,userName,userPhone,userPwd,0,0);
        call.enqueue(new Callback<ResponseBody>() {
            //请求成功时回调
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                //请求处理,输出结果
                try {
                    JSONObject jsonObject = (JSONObject) JSON.parse(response.body().string());
                    String msg =jsonObject.getString("msg");
                    String result = jsonObject.getString("result");
                    String returnobj = jsonObject.getString("obj");
                    JSONObject obj = (JSONObject)JSON.parse(returnobj);

                    if (result.equals("true")){
                        Toast.makeText(RegisterActivity.this,"注册成功！",Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //JsonObject jsonObject = new JsonObject()
            }
            //请求失败时候的回调
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                System.out.println("连接失败");
            }
        });






    }
}

